func c#

71

func c# -

//similar to function in javascript
using System;
public class Program
{	
	public static void Main()
	{
        //Func (returns a non bool value); 
        //1st two int input, last one output type
		Func<int,int, int> Sum = (a,b)=> a+b;
		Console.WriteLine(Sum.Invoke(1,2)); // prints 3 
        
        //Action (void; no value returned); 
      	Action<int,int> Sum = (a,b)=> Console.WriteLine(a+b);
		Sum.Invoke(1,4); //prints 5
      
        //Predicate( Returns a bool)
        Predicate<string> IsGreaterThan5 = (a)=> (a.Length>5)? true: false;
		bool status = IsGreaterThan5.Invoke("Hello World");
		Console.WriteLine(status); // returns true as Length is greater than 5.
	}
}

Comments

Submit
0 Comments